local wu__cikong = fk.CreateSkill {
  name = "wu__cikong",
}

Fk:loadTranslationTable{
  ["wu__cikong"] = "磁控",
  [":wu__cikong"] = "出牌阶段限一次，你可以与一名其他角色<a href='delayedPindian'>延时拼点</a>，然后除非其交给你一张牌，否则你公开结果：若你赢，你弃置其一张牌并对其造成1点伤害。",

  ["delayedPindian"] = "<b>延时拼点：</b><br>拼点牌亮出前，不立刻公布结果，将拼点牌扣置并移出游戏。此回合结束后，若仍未满足公开结果条件，"..
  "将这些拼点牌置入弃牌堆，且不执行后续拼点流程和时机。",

  ["#wu__cikong-choose"] = "磁控：你可以与其他一名角色延时拼点，然后除非其交给你一张牌，否则公开结果：若你赢，你弃置其一张牌并对其造成1点伤害",
  ["#wu__cikong-show"] = "磁控：是否交给 %src 一张牌以防止其公开拼点结果？若其赢则你被弃置一张牌并受到1点伤害",
  ["#wu__cikong-give"] = "磁控：交给 %dest 一张牌",
  ["$wu__cikong-log1"] = "%src发动「磁控」，%s选择交给其一张牌！",
  ["$wu__cikong-log2"] = "%src发动「磁控」，拼点赢过%s，弃置其一张牌并造成1点伤害！",
}

local U = require "packages.utility.utility"

wu__cikong:addEffect("active", {
  anim_type = "control",
  max_phase_use_time = 1,
  card_num = 0,
  target_num = 1,
  can_use = function(self, player)
    return not player:isKongcheng() and player:usedSkillTimes(wu__cikong.name, Player.HistoryPhase) == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, player, to_select, selected, selected_cards)
    return #selected == 0 and to_select ~= player and player:canPindian(to_select)
  end,

  on_use = function(self, room, effect)
    local player = effect.from
    local to = effect.tos[1]
    local pindian = U.delayedPindian(player, {to}, wu__cikong.name)

    if player.dead or to.dead then
      room.logic:getCurrentEvent():findParent(GameEvent.Turn):addCleaner(function()
        U.delayedPindianCleaner(pindian)
      end)
      return
    end

    if room:askToSkillInvoke(to, {
      skill_name = wu__cikong.name,
      prompt = "#wu__cikong-show:"..player.id,
    }) then
      if not to:isKongcheng() then
        local givenCards = room:askToChooseCard(to, {
          target = to,
          flag = "he",
          skill_name = wu__cikong.name,
          prompt = "#wu__cikong-give::"..player.id,
          cancelable = false,
        })

        room:obtainCard(player, givenCards, false, fk.ReasonGive)
        room:sendLog{ type = "skill", content = string.format("$wu__cikong-log1", player.id, to.id) }

        room.logic:getCurrentEvent():findParent(GameEvent.Turn):addCleaner(function()
          U.delayedPindianCleaner(pindian)
        end)
      else
        pindian = U.delayedPindianDisplay(pindian)
        U.delayedPindianCleaner(pindian)

        if pindian.results[to].winner == player and not player.dead and not to.dead then
          local discardCards = room:askToChooseCard(player, {
          target = to,
          flag = "he",
          skill_name = wu__cikong.name,
          cancelable = false,
          })
          room:throwCard(discardCards, wu__cikong.name, to, player)
          room:damage({
            from = player,
            to = to,
            damage = 1,
            skillName = wu__cikong.name,
            reason = fk.ReasonSkill,
          })
          room:sendLog{ type = "skill", content = string.format("$wu__cikong-log2", player.id, to.id) }
        end
      end
    else
      pindian = U.delayedPindianDisplay(pindian)
      U.delayedPindianCleaner(pindian)

      if pindian.results[to].winner == player and not player.dead and not to.dead then
        local discardCards = room:askToChooseCard(player, {
          target = to,
          flag = "he",
          skill_name = wu__cikong.name,
          cancelable = false,
        })
        room:throwCard(discardCards, wu__cikong.name, to, player)
        room:damage({
          from = player,
          to = to,
          damage = 1,
          skillName = wu__cikong.name,
          reason = fk.ReasonSkill,
        })
        room:sendLog{ type = "skill", content = string.format("$wu__cikong-log2", player.id, to.id) }
      end
    end
  end,
})

return wu__cikong